﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Electure.DAL;

namespace Electure.Handlers
{
    /// <summary>
    /// Summary description for UploadFIle
    /// </summary>
    public class UploadFIle : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Files.Count > 0)
            {
                var item = context.Request.Files[0];
                var result = new Dictionary<string, string>();
                using (var rep = RepositoryFactory.GetAttachmentRepository())
                {
                    int fileId = rep.CreateVideoAttachment(item.FileName, item.ContentType, item.InputStream);
                    if (fileId != -1)
                        result.Add(fileId.ToString(), item.FileName);
                }
                context.Response.ContentType = "text/plain";
                context.Response.Write(new JavaScriptSerializer().Serialize(result));
            }
            else
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("Hello World");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}